package com.glory.brepda.activity.mfg;

import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.SuggestShelfListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.PackageEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class PackageUpTubeActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvCircuitName)
    TextView tvCircuitName;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPackageFrom)
    TextView tvPackageFrom;
    @BindView(R.id.tvStates)
    TextView tvStates;
    @BindView(R.id.tvLotNum)
    TextView tvLotNum;
    @BindView(R.id.rvGoodsList)
    RecyclerView rvGoodsList;
    @BindView(R.id.etGoodsNum)
    FreeUI_ClearEditText etGoodsNum;
    @BindView(R.id.rvStorageList)
    RecyclerView rvStorageList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvStorageID)
    TextView tvStorageID;
    @BindView(R.id.tvGrade)
    TextView tvGrade;
    @BindView(R.id.tvListTitle)
    TextView tvListTitle;
    private SuggestShelfListAdapter suggestShelfListAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_package_up;
    }

    @Override
    protected void initView() {
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        setMainLayout(llMain);
        initRecyclerView(rvStorageList,this);
        suggestShelfListAdapter = new SuggestShelfListAdapter(null);
        suggestShelfListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        suggestShelfListAdapter.isFirstOnly(true);
        rvStorageList.setAdapter(suggestShelfListAdapter);

        initEdit(etLot, s -> LotUtils.searchLotInfo(PackageUpTubeActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @Override
            public void onSuccess(LotInfoEntity response) {
                tvCircuitName.setText(response.getPRODUCTMODEL());
                tvCustomCode.setText(response.getLOT().getCUSTOMERCODE());
                tvPackageFrom.setText(response.getLOT().getMAINMATTYPE());
                tvStates.setText(response.getLOT().getSTATE());
                tvLotNum.setText(response.getLOT().getMAINQTY());
                requestPackageEntity(s);
            }

            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
            }
        }));
    }

    private void requestPackageEntity(String s) {
        FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<PackageEntity>>
                (new TypeToken<BaseResponseBean<PackageEntity>>() {
                }.getType(), getSupportFragmentManager(), PackageUpTubeActivity.this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    PackageEntity entity = response.body().getResponse().getBody();
                    tvStorageID.setText(entity.getSTORAGEID());
                    suggestShelfListAdapter.setNewData(entity.getSUGGESTSHELFLIST());
                    tvListTitle.setText(entity.getSUGGESTSHELFLIST().size() > 0 ? "空货架清单" + entity.getSUGGESTSHELFLIST().size() + "个" : "空货架清单");
                    tvGrade.setText(response.body().getResponse().getBody().getBINGRADE());
                    String sampleinfo = response.body().getResponse().getBody().getSAMPLEINFO();
                    if (!sampleinfo.equals("")){
                        showConfirmDialog(sampleinfo);
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "PACKAGEUPSHELFTUBE");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "BINDSELECT");
        map.addProperty("LOTID", s);
        NetUtils.requestPostNet("CPC.PACKAGEUPSHELFTUBE", "PACKAGEUPSHELFTUBE", PackageUpTubeActivity.this, map, callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (getText(etLot).equals("") || getText(etGoodsNum).equals("")) {
            showToast("批次号和货位号不能为空", false);
            return;
        }

        if (!tvStorageID.getText().equals("")) {
            if (!getText(etGoodsNum).equals(tvStorageID.getText())){
                showToast("必须上架推荐货位!",false);
                return;
            }
        }

        showSubmitDialog(new onSubmitListener() {
            @Override
            public void setOnSubmitListener() {
                FreeUI_EntityCallBack<BaseResponseBean<PackageEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<PackageEntity>>
                        (new TypeToken<BaseResponseBean<PackageEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<PackageEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                            PackageEntity body = response.body().getResponse().getBody();
                            showConfirmDialog(body.getSAMPLEINFO().equals("") ? "上架成功" : body.getSAMPLEINFO());
                            clear();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<PackageEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(),"PACKAGEUPSHELFTUBE");

                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("ACTIONTYPE","BIND");
                map.addProperty("LOTID",getText(etLot));
                map.addProperty("STORAGEID",getText(etGoodsNum));
                NetUtils.requestPostNet("CPC.PACKAGEUPSHELFTUBE","PACKAGEUPSHELFTUBE",PackageUpTubeActivity.this,map,callBack);
            }
        });
    }

    private void clear(){
        etGoodsNum.setText("");
        etLot.setText("");
        tvStorageID.setText("");
        tvCircuitName.setText("");
        tvCustomCode.setText("");
        tvPackageFrom.setText("");
        tvLotNum.setText("");
        tvStates.setText("");
        etLot.requestFocus();
        suggestShelfListAdapter.getData().clear();
        suggestShelfListAdapter.notifyDataSetChanged();
    }
}
